#![deny(warnings)]


use std::io::{stdin};
use std::time::SystemTime;

async fn req() {
    let mut red = String::new();
    stdin().read_line(&mut red).expect("输入错误！");
    let paths = vec![
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://www.uc123.com/".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
        "https://cloud.tencent.com/developer/article/1554809".to_string(),
    ];

    let now =SystemTime::now();
    println!("异步开始req数量：{}", paths.len());
    let mut tasks: Vec<tokio::task::JoinHandle<Result<(), ()>>>= vec![];
    for url in paths {
        let _url = url.clone();
        tasks.push(tokio::spawn(async move {
            let client = reqwest::Client::new();
            let resp_future =  client.get(&_url).send();
            if let Ok(resp) = resp_future.await {
                match resp.text().await {
                    Ok(text) => {
                        println!("请求结果: {} bytes from {}", text.len(), _url);
                    }
                    Err(_) => println!("ERROR reading {}", _url),
                }
            }
            Ok(())
        }));

    }
    futures::future::join_all(tasks).await;
    // use futures::StreamExt;
    // let fetches = futures::stream::iter(
    //     paths.iter().map(|path| {
    //         async move {
    //             match reqwest::get(path).await {
    //                 Ok(resp) => {
    //                     match resp.text().await {
    //                         Ok(text) => {
    //                             println!("RESPONSE: {} bytes from {}", text.len(), path);
    //                         }
    //                         Err(_) => println!("ERROR reading {}", path),
    //                     }
    //                 }
    //                 Err(_) => println!("ERROR downloading {}", path),
    //             }
    //         }
    //     })
    // ).buffer_unordered(15).collect::<Vec<()>>();
    // println!("Waiting...");
    // fetches.await;
    println!("异步结束req： {:?}", SystemTime::now().duration_since(now));
    stdin().read_line(&mut red).expect("输入错误！");
}

#[tokio::main]
 async fn main()  {
    req().await;
}